<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header('修改部门')" />
	<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/layui-src@2.5.5/dist/css/layui.css"  media="all">
</head>
<body class="white-bg">
	<div class="wrapper wrapper-content animated fadeInRight ibox-content">
		<form class="form-horizontal m" id="form-dept-edit" th:object="${obj}">
			<input name="id" type="hidden" th:value="*{id}" />
			<input id="treeId" name="pid" type="hidden" th:value="*{pid}" />
			<div class="form-group">
				<label class="col-sm-3 control-label ">上级部门：</label>
				<div class="col-sm-8">
				    <div class="input-group">
						<input class="form-control" type="text" id="treeName" onclick="selectDeptTree()" th:value="*{pname}" readonly="true" >
					    <span class="input-group-addon"><i class="fa fa-search"></i></span>
				    </div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">系统标题：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="title" th:value="*{title}" id="title" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">机构名称：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="name" th:value="*{name}" id="deptName" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">机构全称：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="corp" th:value="*{corp}" id="corp" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">唯一代码：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="code" th:value="*{code}" id="code" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">负责人：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="leader" th:value="*{leader}">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">联系电话：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" maxlength="11" name="phone" th:value="*{phone}">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">备注：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="text" th:value="*{text}">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">省份：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="province" th:value="*{province}">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">城市：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="city" th:value="*{city}">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">区/县：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="county" th:value="*{county}">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">单位类型：</label>
				<div class="col-sm-8">
					<div class="radio-box">
						<input type="radio" id="statushide" required name="type" value="0" th:checked="*{type==0}" >
						<label for="4">体育局</label>
					</div>
					<div class="radio-box">
						<input type="radio" id="statusshow" required name="type" value="1" th:checked="*{type==1}">
						<label for="5">厂家</label>
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">二级域名：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="domain" th:value="*{domain}">
				</div>
			</div>
			<div class="form-group">
				<input type="hidden" name="logo" id="logo" th:value="*{logo}">
				<label class="col-sm-3 control-label">logo：</label>
				<div class="col-sm-8">
					<div class="layui-upload">
					  <button type="button" class="layui-btn" id="test1">上传图片</button>
					  <div class="layui-upload-list">
					    <img class="layui-upload-img" style="width: 100px;height: 100px;" id="demo1">
					    <p id="demoText"></p>
					  </div>
					</div>   
				</div>
			</div>
			
		</form>
	</div>
	<th:block th:include="include :: footer" />
	<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/layui-src@2.5.5/dist/layui.js"></script>
	<script type="text/javascript">
		var prefix = ctx + "sys/unit";
		
		$("#form-dept-edit").validate({
			onkeyup: false,
			rules:{
				deptName:{
					remote: {
		                url: prefix + "/checkDeptNameUnique",
		                type: "post",
		                dataType: "json",
		                data: {
		                	"deptId": function() {
		                        return $("#deptId").val();
		                    },
		                    "parentId": function() {
		                        return $("input[name='parentId']").val();
		                    },
		        			"deptName": function() {
		                        return $.common.trim($("#deptName").val());
		                    }
		                },
		                dataFilter: function(data, type) {
		                	return $.validate.unique(data);
		                }
		            }
				},
				orderNum:{
					digits:true
				},
				email:{
                    email:true,
        		},
        		phone:{
        			isPhone:true,
        		},
			},
			messages: {
		        "deptName": {
		            remote: "部门已经存在"
		        }
		    },
		    focusCleanup: true
		});
		
		function submitHandler() {
	        if ($.validate.form()) {
	        	$.operate.save(prefix + "/save", $('#form-dept-edit').serialize());
	        }
	    }
	
		/*部门管理-修改-选择部门树*/
		function selectDeptTree() {
			var deptId = $("#treeId").val();
			if(deptId > 0) {
			    var options = {
					title: '部门选择',
					width: "380",
					url: prefix + "/unit?id=" + $("#treeId").val(),
					callBack: doSubmit
				};
				$.modal.openOptions(options);
			} else {
        		$.modal.alertError("父部门不能选择");
        	}
		}
		
		function doSubmit(index, layero){
			var tree = layero.find("iframe")[0].contentWindow.$._tree;
			if ($.tree.notAllowLastLevel(tree)) {
	   			var body = layer.getChildFrame('body', index);
	   			$("#treeId").val(body.find('#treeId').val());
	   			$("#treeName").val(body.find('#treeName').val());
	   			layer.close(index);
			}
		}
		
		layui.use('upload', function(){
			  var $ = layui.jquery
			  ,upload = layui.upload;
			  
			  //普通图片上传
			  var uploadInst = upload.render({
			    elem: '#test1'
			    ,url: '/api/up' //改成您自己的上传接口
			    ,before: function(obj){
			      //预读本地文件示例，不支持ie8
			      obj.preview(function(index, file, result){
			        $('#demo1').attr('src', result); //图片链接（base64）
			      });
			    }
			    ,done: function(res){
			      //如果上传失败
			      if(res.code > 0){
			        return layer.msg('上传失败');
			      }
			      //上传成功
			      $("#logo").val(res.file)
			    }
			    ,error: function(){
			      //演示失败状态，并实现重传
			      var demoText = $('#demoText');
			      demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
			      demoText.find('.demo-reload').on('click', function(){
			        uploadInst.upload();
			      });
			    }
			  });
		})
	</script>
</body>
</html>
